﻿/**
 * Copyright (c) 2008 Justin Etheredge http://www.codethinked.com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Microsoft Public License (Ms-PL)
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/ms-pl.html
 * If redistributing this code, this entire header must remain intact.
 * 
 * Some of the code in this application has been taken 
 * from the following sources (all of which are licensed under
 * the Ms-PL):
 * Matt Warren's Blog: http://blogs.msdn.com/mattwar/
 * MSDN: http://msdn2.microsoft.com/en-us/library/bb546158.aspx
 */

using LinqToSimpleDB;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Collections;
using System.Linq;

namespace LinqToSimpleDBTest
{
  /// <summary>
  ///This is a test class for SimpleDBAttributeCollectionTest and is intended
  ///to contain all SimpleDBAttributeCollectionTest Unit Tests
  ///</summary>
  [TestClass()]
  public class SimpleDBAttributeCollectionTest
  {


    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion


    /// <summary>
    ///A test for System.Collections.IEnumerable.GetEnumerator
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LinqToSimpleDB.dll")]
    public void GetEnumeratorTest()
    {
      var target = new SimpleDBAttributeCollection();
      var attribute1 = new SimpleDBAttribute("test1", "test2", "test3");
      var attribute2 = new SimpleDBAttribute("test4", "test5", "test6");
      var attribute3 = new SimpleDBAttribute("test7", "test8", "test9");

      target.Add(attribute1);
      target.Add(attribute2);
      target.Add(attribute3);

      Assert.IsTrue(target.Count == 3);

      int count = 1;
      foreach (SimpleDBAttribute attribute in target)
      {
        if (count == 1)
          Assert.AreEqual(attribute1, attribute);
        else if (count == 2)
          Assert.AreEqual(attribute2, attribute);
        else if (count == 3)
          Assert.AreEqual(attribute3, attribute);

        count++;
      }
      Assert.IsTrue(count == 4, "Actual: {0} Expected: {1}", count, 4);

      IEnumerable target2 = target;
      count = 1;
      foreach (SimpleDBAttribute attribute in target2)
      {
        if (count == 1)
          Assert.AreEqual(attribute1, attribute);
        else if (count == 2)
          Assert.AreEqual(attribute2, attribute);
        else if (count == 3)
          Assert.AreEqual(attribute3, attribute);

        count++;
      }
      Assert.IsTrue(count == 4, "Actual: {0} Expected: {1}", count, 4);
    }

    /// <summary>
    ///A test for SimpleDBAttributeCollection Constructor
    ///</summary>
    [TestMethod()]
    public void SimpleDBAttributeCollectionConstructorTest()
    {
      SimpleDBAttributeCollection target = new SimpleDBAttributeCollection();
      Assert.IsNotNull(target);
      Assert.IsNotNull(target.Items);
      Assert.IsTrue(target.Items.Count() == 0);
    }

    /// <summary>
    ///A test for LinqToSimpleDB.IDirtyTrackable.IsDirty
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LinqToSimpleDB.dll")]
    public void IsDirtyTest()
    {
      IDirtyTrackable target = new SimpleDBAttributeCollection();

      //AttributeCollection should ignore MarkClean and MarkDirty because
      //it only cares about it contents
      Assert.IsFalse(target.IsDirty);
      target.MarkDirty();
      Assert.IsFalse(target.IsDirty);

      var attribute = new SimpleDBAttribute("test1");
      ((SimpleDBAttributeCollection)target).Add(attribute);
      
      Assert.IsTrue(target.IsDirty);
      target.MarkClean();
      Assert.IsTrue(target.IsDirty);

      ((IDirtyTrackable)attribute).MarkClean();
      Assert.IsFalse(target.IsDirty);
    }

    /// <summary>
    ///A test for Item
    ///</summary>
    [TestMethod()]
    public void ItemAddAndRemoveTest()
    {
      SimpleDBAttributeCollection target = new SimpleDBAttributeCollection(); // TODO: Initialize to an appropriate value
      var attribute1 = new SimpleDBAttribute("test1", "test");
      var attribute2 = new SimpleDBAttribute("test2", "test");
      var attribute3 = new SimpleDBAttribute("test3", "test");

      target.Add(attribute1);
      target.Add(attribute2);
      target.Add(attribute3);

      Assert.IsTrue(target["test1"] == attribute1);
      Assert.IsTrue(target["test2"] == attribute2);
      Assert.IsTrue(target["test3"] == attribute3);

      Assert.IsTrue(target.Remove(attribute1));
      Assert.IsTrue(target.Remove(attribute2));
      Assert.IsFalse(target.Remove(attribute2));

      Assert.IsTrue(target.Count == 1);

      Assert.IsTrue(target.Remove(attribute3));

      Assert.IsTrue(target.Count == 0);
    }
  }
}
